﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebManager;
using Maticsoft.DBUtility;//请先添加引用

namespace BaoJian.WebManager.Manager.RelateCol
{
    public partial class Edit : System.Web.UI.Page
    {
        string r = null;
        string p = null;
        string pg = null;
        LYQ.BLL.RelatedColumn bllCol;
        LYQ.Model.RelatedColumn modelCol;
        LYQ.BLL.RelateImage bllImage;
        LYQ.Model.RelateImage modelImg;       
        protected void Page_Load(object sender, EventArgs e)
        {
            r = HttpContext.Current.Request.QueryString["region"];
            pg = HttpContext.Current.Request.QueryString["page"];
            p = HttpContext.Current.Request.QueryString["PriID"];
            if (r == null || p == null || pg == null)             
            {
                Page.Response.Redirect("../../ErrorMsg.aspx");
            }
            if (!Page.IsPostBack)
            {
                
                BindPage();
                BindRegion();
                BindData();
            }
        }

        private void BindPage()
        {           
            SelectPage.Text = pg;

        }
        private void BindRegion()
        {
            LYQ.BLL.S_Group groupbll = new LYQ.BLL.S_Group();
            Region.DataSource = groupbll.GetList("GName !='系统管理员'").Tables[0];
            Region.DataBind();
            ListItem item = Region.Items.FindByValue(r);
            item.Selected = true;
            Region.Enabled = false;

        }
        private void BindData()
        {
            LYQ.BLL.SysObj sysbll = new LYQ.BLL.SysObj();
            string sql = " SELECT [SysObj].oid,oname FROM [SysObj] INNER  JOIN [SysObj_Hiber] ON [SysObj].[OID] = [SysObj_Hiber].[OID] "
                           + " WHERE [SysObj_Hiber].[ParentID]='{0}'";
            if (Region.SelectedValue == "beijing")
            {
                sql = string.Format(sql, "eb8b49b9-e760-497b-83cf-d9fd1d9f0833");
            }
            else
            {
                sql = string.Format(sql, "FD870D78-0DB5-48D5-AF31-4D6ACC3FA90D");
            }

            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            if (dt == null)
            {
                Page.Response.Redirect("../../ErrorMsg.aspx");
            }
            else 
            {
                PriColumnID.DataSource = dt;
                PriColumnID.DataBind();        

                AuxiColumnID1.DataSource = dt;
                AuxiColumnID1.DataBind();

                AuxiColumnID2.DataSource = dt;
                AuxiColumnID2.DataBind();


                AuxiColumnID3.DataSource = dt;
                AuxiColumnID3.DataBind();

                AuxiColumnID4.DataSource = dt;
                AuxiColumnID4.DataBind();
            }

            SetValue(PriColumnID, p);
            PriColumnID.Enabled = false;

            bllCol = new LYQ.BLL.RelatedColumn();
            modelCol = bllCol.GetModel(pg, p,r);
            if (modelCol == null)             
            {
                Page.Response.Redirect("../../ErrorMsg.aspx");
            }

            if (modelCol.SumAuxi > 0)
                SetValue(AuxiColumnID1, modelCol.AuxiColumnID1);
            if (modelCol.SumAuxi > 1)
                SetValue(AuxiColumnID2, modelCol.AuxiColumnID2);
            if (modelCol.SumAuxi > 2)
                SetValue(AuxiColumnID3, modelCol.AuxiColumnID3);
            if (modelCol.SumAuxi > 3)
                SetValue(AuxiColumnID4, modelCol.AuxiColumnID4);

            string path = Server.MapPath(@"~/uploads/ad/" + Region.SelectedValue);
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
            System.IO.FileInfo[] info = dir.GetFiles();
            DataTable temp = MakeTable(info);

            ImagesList1.DataSource = temp;
            ImagesList1.DataBind();

            ImagesList2.DataSource = temp;
            ImagesList2.DataBind();

            ImagesList3.DataSource = temp;
            ImagesList3.DataBind();


            ImagesList4.DataSource = temp;
            ImagesList4.DataBind();

            ImagesList5.DataSource = temp;
            ImagesList5.DataBind();

            bllImage = new LYQ.BLL.RelateImage();
            modelImg = bllImage.GetModel(pg, p, r);
            if (modelImg == null)             
            {
                Page.Response.Redirect("../../ErrorMsg.aspx");
            }

            if (modelImg.SumImage > 0)
            {
                Image1.Text = modelImg.ImagePath1;
                URL1.Text = modelImg.URL1;
                SetValue(ImagesList1,modelImg.ImagePath1);
            }
            if (modelImg.SumImage > 1)
            {
                Image2.Text = modelImg.ImagePath2;
                URL2.Text = modelImg.URL2;
                SetValue(ImagesList2, modelImg.ImagePath2);
            } 
            if (modelImg.SumImage > 2)
            {
                Image3.Text = modelImg.ImagePath3;
                URL3.Text = modelImg.URL3;
                SetValue(ImagesList3, modelImg.ImagePath3);
            }
            if (modelImg.SumImage > 3)
            {
                Image4.Text = modelImg.ImagePath4;
                URL4.Text = modelImg.URL4;
                SetValue(ImagesList4, modelImg.ImagePath4);
            } 
            if (modelImg.SumImage > 4)
            {
                Image5.Text = modelImg.ImagePath5;
                URL5.Text = modelImg.URL5;
                SetValue(ImagesList5, modelImg.ImagePath5);
            }

            txtDescription.Text = modelCol.Description;
        }


        private DataTable MakeTable(System.IO.FileInfo[] info)
        {

            System.Data.DataTable table = new DataTable("Table");

            DataColumn column;
            DataRow row;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "filename";
            column.ReadOnly = true;
            column.Unique = true;
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "filevalue";

            table.Columns.Add(column);
            foreach (System.IO.FileInfo file in info)
            {
                row = table.NewRow();
                row["filename"] = file.Name;
                row["filevalue"] = file.Name;
                table.Rows.Add(row);
            }

            return table;
        }

        private void SetValue(DropDownList drpList, string value)
        {
            ListItem item = drpList.Items.FindByValue(value);
            item.Selected = true;
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            UpdateRelatedColumn();
            UpdateRelateImage();
            string url = URLUtility.UrlBase + @"/WebManager/manager/RelateCol/List.aspx";
            Page.Response.Redirect(url);
        }

        private void UpdateRelatedColumn()
        {
            LYQ.BLL.RelatedColumn bll = new LYQ.BLL.RelatedColumn();
            LYQ.Model.RelatedColumn model = bll.GetModel(pg, p, r);
            if (model == null)
            {
                Page.Response.Redirect("../../errormsg.aspx");
            }

            model.PriColumnID = PriColumnID.SelectedValue;
            model.SumAuxi = 0;
            //model.SID = Tools.Common.GetMaxSIDAddedONE("RelatedColumn");
            model.Region = Region.SelectedValue;
            model.Page = SelectPage.Text;
            model.Description = txtDescription.Text;
            model.AuxiColumnID1 = "";
            model.AuxiColumnID2 = "";
            model.AuxiColumnID3 = "";
            model.AuxiColumnID4 = "";
            model.AuxiColumnID5 = "";
            model.AuxiColumnID6 = "";

            if (AuxiColumnID1.SelectedValue != "-1")
            {
                model.SumAuxi++;
                model.AuxiColumnID1 = AuxiColumnID1.SelectedValue;
            }
            if (AuxiColumnID2.SelectedValue != "-1")
            {
                model.SumAuxi++;
                model.AuxiColumnID2 = AuxiColumnID2.SelectedValue;
            }

            if (AuxiColumnID3.SelectedValue != "-1")
            {
                model.SumAuxi++;
                model.AuxiColumnID3 = AuxiColumnID3.SelectedValue;
            }
            if (AuxiColumnID4.SelectedValue != "-1")
            {
                model.SumAuxi++;
                model.AuxiColumnID4 = AuxiColumnID4.SelectedValue;
            }

            model.Description = txtDescription.Text;
            bll.Update(model);
        }

        private void UpdateRelateImage()
        {
            LYQ.BLL.RelateImage bll = new LYQ.BLL.RelateImage();
            LYQ.Model.RelateImage model = bll.GetModel(pg, p, r);
            if (model == null)
            {
                Page.Response.Redirect("../../errormsg.aspx");
            }

            model.PriColumnID = PriColumnID.SelectedValue;
            model.SID = Tools.Common.GetMaxSIDAddedONE("RelateImage");
            model.Region = Region.SelectedValue;
            model.Page = SelectPage.Text;
            model.Description = txtDescription.Text;
            model.SumImage = 0;
            model.ImagePath1 = "";
            model.URL1 = "";
            model.ImagePath2 = "";
            model.URL3 = "";
            model.ImagePath4 = "";
            model.URL3 = "";
            model.ImagePath4 = "";
            model.URL4 = "";
            model.ImagePath5 = "";
            model.URL5 = "";
            model.ImagePath6 = "";
            model.URL6 = "";

            if (Image1.Text != "")
            {
                model.ImagePath1 = Image1.Text;
                model.URL1 = URL1.Text;
                model.SumImage++;
            }

            if (Image2.Text != "")
            {
                model.ImagePath2 = Image2.Text;
                model.URL2 = URL2.Text;
                model.SumImage++;
            }

            if (Image3.Text != "")
            {
                model.ImagePath3 = Image3.Text;
                model.URL3 = URL3.Text;
                model.SumImage++;
            }


            if (Image4.Text != "")
            {
                model.ImagePath4 = Image4.Text;
                model.URL4 = URL4.Text;
                model.SumImage++;
            }

            if (Image5.Text != "")
            {
                model.ImagePath5 = Image5.Text;
                model.URL5 = URL5.Text;
                model.SumImage++;
            }
            bll.Update(model);
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + @"/WebManager/manager/RelateCol/List.aspx";
            Page.Response.Redirect(url);
        }

        protected void ImagesList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image1.Text = ImagesList1.SelectedValue;
            SetImagePath(ImagesList1.SelectedValue);
        }
        protected void ImagesList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image2.Text = ImagesList2.SelectedValue;
            SetImagePath(ImagesList2.SelectedValue);
        }
        protected void ImagesList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image3.Text = ImagesList3.SelectedValue;
            SetImagePath(ImagesList3.SelectedValue);
        }
        protected void ImagesList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image4.Text = ImagesList4.SelectedValue;
            SetImagePath(ImagesList4.SelectedValue);
        }
        protected void ImagesList5_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image5.Text = ImagesList5.SelectedValue;
            SetImagePath(ImagesList5.SelectedValue);
        }
        private void SetImagePath(string imgName)
        {
            string path = @"~/uploads/ad/" + Region.SelectedValue;
            path += @"/" + imgName;
            ShowImage.ImageUrl = path;
        }




    }
}
